/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import org.ipti.br.tag.entity.enums.Reference;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */

@Entity
public class Performance extends AbstractEntity{
    
    @ManyToOne
    @JoinColumn(name = "actor_id")
    private Actor actor;
    
    @ManyToOne
    @JoinColumn(name = "discipline_id")
    private Discipline discipline;
    
    @Enumerated(EnumType.ORDINAL)
    private Reference reference;

    @Column(name="reference_id")
    private Long referenceID;
    
    @Column(precision = 2)
    @Digits(integer = 2, fraction = 2, message="Valor deve ter no máximo 4 dígitos (ex: 10.00)")
    @DecimalMax(value="10.00", message="Valor deve ser menor ou igual a 10.00")
    @DecimalMin(value="0.00", message="Valor deve ser maior ou igual a 0.00")
    private float grade;
    
    @Column(name="seq")
    private Integer seq;

    public Performance() {
        super();
    }

    @Override
    public void generateHash(){
        if(getId() == null){
            String hash = ""
                    + ((this.getActor() != null) ? this.getActor().getId() : null)
                    + ((this.getDiscipline() != null) ? this.getDiscipline().getId() : null)
                    + this.getReference()
                    + this.getReferenceID()
                    + this.getSeq();
            this.setId(Long.valueOf(EncryptionUtil.encrypt(hash).hashCode()));
        }
    }

    public Performance(Actor actor, Discipline discipline, Reference reference, Long referenceID, float grade) {
        this.actor = actor;
        this.discipline = discipline;
        this.reference = reference;
        this.referenceID = referenceID;
        this.grade = grade;
    }

    public Actor getActor() {
        return actor;
    }

    public void setActor(Actor actor) {
        this.actor = actor;
    }

    public Discipline getDiscipline() {
        return discipline;
    }

    public void setDiscipline(Discipline discipline) {
        this.discipline = discipline;
    }

    public float getGrade() {
        return grade;
    }

    public void setGrade(float grade) {
        this.grade = grade;
    }

    public Reference getReference() {
        return reference;
    }

    public void setReference(Reference reference) {
        this.reference = reference;
    }

    public Long getReferenceID() {
        return referenceID;
    }

    public void setReferenceID(Long referenceID) {
        this.referenceID = referenceID;
    }
    public Integer getSeq() {
        return seq;
    }

    public void setSeq(Integer seq) {
        this.seq = seq;
    }
    
}
